package net.butfly.albacore.dao.plugin;

import java.util.Map;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

@Intercepts({ @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
		RowBounds.class, ResultHandler.class }) })
public class MySqlPaginationPlugin extends PaginationBasePlugin {
	@SuppressWarnings("rawtypes")
	@Override
	protected String paginationSql(String sql, Object args) {
		if (args instanceof Map) {
			if (((Map) args).containsKey("pagination")) {
				Map map = (Map) args;
				RowBounds rowBounds = (RowBounds) map.get("pagination");
				sql = sql + " limit " + rowBounds.getOffset() + "," + rowBounds.getLimit(); // 分页了
			}
		}
		return sql;
	}
}